<?php
/**
 * Created by PhpStorm.
 * User: song
 * Date: 2018/8/15
 * Time: 上午10:54
 */


namespace app\model;

use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
use QingPHP\Lib\MysqlLib;
use think\facade\Db;

class HostPortModel extends BaseModel
{

    public static $tableName = 'host_port';


    /**
     * @param int $id
     * @param string $url
     * @param string $callUrl
     * @throws Exception
     */
    public static function sendTask(int $id, string $url)
    {
        $rabbitConf = getRabbitMq();
        $connection = new AMQPStreamConnection($rabbitConf['host'], $rabbitConf['port'], $rabbitConf['user'], $rabbitConf['password'], $rabbitConf['vhost']);
        $channel = $connection->channel();

        $queueName = "rad";
        $channel->queue_declare($queueName, false, false, false, false);

        //发送任务到节点
        $data = [
            'id' => $id,
            'url' => $url
        ];

        $sendData = json_encode($data);

        $msg = new AMQPMessage($sendData);
        $result = $channel->basic_publish($msg, '', $queueName);

        addlog(['发送爬虫任务', $id, $url, $data]);


        $channel->close();
        $connection->close();

    }


    public static function getCrawlerInfo($crawlerId)
    {

        //查询具体数据,并刷新缓存
        $result = self::getList(['id' => $crawlerId]);


        return $result[0] ?? false;

    }

    /**
     * 获取APP的URL地址
     *
     * @param int $appId
     * @return mixed
     */
    public static function getCrawlerList(int $appId)
    {

        //查询具体数据,并刷新缓存
        $result = self::getList(['app_id' => $appId]);


        return $result;

    }

    /**
     * @param  $where
     * @param int $limit
     * @param array $otherParam
     * @return mixed
     */
    private static function getList($where, int $limit = 15, int $page = 1, array $otherParam = [])
    {

        $order = ['id' => 'desc'];
        $result = Db::table(self::$tableName)->where($where)->page($page)->order($order)->select()->toArray();

        return $result;
    }

    private static function getCount($where, array $otherParam = [])
    {
        return 0;
        $db = new MysqlLib(getMysql());
        $group = $otherParam['group'] ?? '';

        $db = $db->table(self::$tableName);

        if ($group) {
            $db->group($group);
        }

        $result = $db->where($where)->count();


        return $result[0]['num'] ?? 0;
    }

    private static function getServiceList($where)
    {

        $list = Db::table(self::$tableName)
            ->field('id,service as name,count(service) as num')
            ->where($where)
            ->group('service')
            ->having("num > 50")
            ->select()
            ->toArray();
        array_multisort(array_column($list, 'num'), SORT_DESC, $list);

        $list = array_slice($list, 0, 10);
        return $list;
    }

    private static function getPort($where)
    {
        $list = Db::table(self::$tableName)
            ->field('id,port as name,count(port) as num')
            ->where($where)
            ->group('port')
            ->having("num > 50")
            ->select()
            ->toArray();
        array_multisort(array_column($list, 'num'), SORT_DESC, $list);

        $list = array_slice($list, 0, 10);
        return $list;
    }

    public static function getClassify($where)
    {

        $data = [
            ['服务', HostPortModel::getServiceList($where), "service"],
            ['端口排名', HostPortModel::getPort($where), "port"],
        ];


        return $data;
    }

    public static function getListByWherePage($where, $page, $pageSize = 15)
    {

        $list = self::getList($where, $pageSize, $page);

        $count = self::getCount($where);

        return ['list' => $list, 'count' => $count, 'pageSize' => $pageSize];
    }

    public static function getListByWhere($where)
    {

        $list = self::getList($where);

        return $list;
    }

    public static function getHttpList()
    {
        $list = Db::table(self::$tableName)
            ->whereIn('service', "http,https")
            ->limit(100)
            ->order('id', 'desc')
            ->select()
            ->toArray();

        return $list;
    }

    /**
     * 获取单条记录
     *
     * @param int $id
     * @return array
     */
    public static function getInfo(int $id)
    {
        $where = ['id' => $id];

        $list = self::getList($where);

        return $list[0] ?? [];
    }

    /**
     * 内部方法，更新数据
     *
     * @param array $where
     * @param array $data
     * @return mixed
     */
    public static function updateByWhere(array $where, array $data)
    {
        $result = Db::name(self::$tableName)
            ->where($where)
            ->update($data);
        return $result;
    }

    /**
     * 更新生成任务状态
     *
     * @param string $crawlerNum
     * @param int $status
     */
    public static function updateStatus(int $id, int $status)
    {
        $where = ['id' => $id];
        $data = ['status' => $status];
        self::updateByWhere($where, $data);
    }


    public static function updateScanStatus(int $id, int $status, string $cmdResult)
    {
        $where = ['id' => $id];
        $data = ['scan_status' => $status, 'cmd_result' => $cmdResult];
        self::updateByWhere($where, $data);
    }

    public static function updateCrawlStatus(int $id, int $status)
    {
        $where = ['id' => $id];
        $data = ['crawl_status' => $status];
        self::updateByWhere($where, $data);
    }

    /**
     * @param array $data
     */
    public static function addData(array $data, $metod = 'get')
    {
        $data['method'] = $metod;


        return self::add($data);
    }

    private static function add($data)
    {

        addlog(["保存URL地址", $data]);

        Db::table(self::$tableName)->insert($data);
    }


    public static function NmapPortScan()
    {
        while (true) {
            processSleep(1);
            $taskList = Db::table('host_port')->where(['service' => null])->orderRand()->limit(10)->select()->toArray();
            foreach ($taskList as $value) {
                PluginModel::addScanLog($value['id'], __METHOD__, 0,1);
                $result = [];
                $cmd = "nmap -sS -Pn -T4  -p {$value['port']} {$value['host']} | grep open | grep -v Discovered |grep -v grep";
                echo $cmd . PHP_EOL;
                execLog($cmd, $result);

                foreach ($result as $item) {
                    $item = str_replace("  ", " ", $item);
                    $aaa = explode(" ", $item);
                    if (count($aaa) != 3) {
                        continue;
                    }

                    $data = ['service' => $aaa[2]];

                    $where = ['host' => $value['host'], 'port' => $value['port']];
                    Db::table('host_port')->where($where)->update($data);
                }
                PluginModel::addScanLog($value['id'], __METHOD__, 1,1);
            }
            sleep(10);
        }
    }

    public static function MasFastScan()
    {

        $ip = "34.214.44.64,54.167.112.153,3.87.174.69,54.149.116.210,54.185.35.208,34.215.46.178,18.209.48.144,18.237.39.39,34.219.225.40,44.242.155.244,35.162.246.150,54.68.198.223,34.211.121.146,34.217.135.245,54.244.57.168,39.106.204.20,18.237.165.216,54.213.195.62,23.22.145.195,52.37.157.62,34.219.231.63,34.211.141.138,18.237.154.225,3.87.70.227,54.212.73.144,54.244.181.139,34.216.0.216,35.161.202.182,54.185.38.200,54.148.197.159,35.155.235.166,18.206.227.95,54.214.183.144,54.186.82.10,34.222.177.218,18.237.169.126,54.172.112.238,34.209.23.66,54.172.240.6,54.223.65.82,44.235.91.74,54.204.121.176,54.213.131.139,3.84.223.110,47.251.21.49,35.80.143.224,54.167.103.244,34.211.20.145,54.190.226.66,35.164.35.13,34.214.55.235,54.184.107.98,54.191.173.99,54.188.94.60,3.85.3.59,34.222.36.68,170.106.197.192,43.130.3.129,3.83.25.254,54.69.174.102,54.202.84.218,52.34.211.183,54.244.96.16,34.210.74.35,18.209.57.67,54.158.29.25,35.155.240.27,54.222.183.49,54.84.36.192,54.201.11.18,18.237.168.187,54.203.49.43,54.227.190.187,52.81.132.94,52.23.213.120,34.220.88.196,34.203.200.222,34.227.221.16,35.162.106.122,34.213.217.81,54.184.220.204,52.91.3.34,52.41.247.201,54.84.252.224,54.226.27.168,54.202.130.154,52.201.164.227,34.219.246.250,54.90.202.174,3.85.93.146,54.226.168.20,34.233.126.128,100.20.81.221,52.204.162.51,54.175.69.45,34.205.31.165,52.203.227.128,52.80.208.217,140.179.8.227,54.149.254.54,44.239.131.40,34.222.54.202,52.90.156.213,34.221.83.254,34.203.21.44,52.81.2.11,34.222.229.101,54.222.202.249,35.163.35.215,35.162.169.92,18.204.16.24,18.237.16.13,54.172.174.19,54.190.162.249,54.227.151.34,52.13.22.241,34.229.167.67,52.37.96.28,54.90.172.28,52.81.72.254,107.21.56.74,52.81.163.13,52.37.27.67,204.236.220.235,54.185.3.164,54.202.119.46,52.26.3.118,34.220.119.0,34.212.40.200,52.24.84.171,54.185.2.125,3.95.168.78,34.216.178.147,52.38.231.183,54.166.43.225,18.206.236.185,52.33.233.147,54.186.113.182,54.245.64.121,44.233.146.158,52.41.93.119,3.84.31.212,54.226.36.120,54.91.3.185,34.217.108.212,34.219.128.181,52.32.232.142,52.81.242.122,52.39.206.196,34.220.79.46,54.174.183.206,54.203.197.199,52.81.72.168,54.201.31.183,54.191.252.159,18.237.34.121,34.219.213.101,54.191.182.26,3.85.108.61,34.217.0.247,35.172.214.135,18.237.58.131,52.80.108.232,34.221.39.189,52.32.178.179,3.83.127.21,52.37.182.23,54.190.26.20,54.185.109.157,54.189.65.220,18.237.36.144,3.87.40.42,34.221.129.22,44.228.76.233,54.149.47.138,54.174.219.201,3.80.44.0,54.191.196.173,54.209.84.173,52.206.233.151,52.80.4.42,34.213.3.14,54.212.138.66,54.185.80.227,3.90.215.90,54.214.198.10,54.223.35.15,35.163.240.60,52.33.200.131,3.84.128.212,18.234.225.30,54.191.91.54,3.88.63.251,3.88.173.157,35.166.182.139,34.239.110.75,54.201.236.91,34.217.55.134,54.85.58.222,52.39.200.71,52.80.94.197,34.216.76.76,54.222.254.252,3.88.215.82,54.89.159.193,35.163.43.223,34.214.85.249,34.208.196.199,52.88.195.142,54.201.129.255,54.144.59.1,54.245.134.118,54.223.188.83,18.237.156.22,100.26.184.236,34.208.68.55,34.207.88.136,34.209.143.53,3.89.112.200,54.222.236.161,54.190.111.56,34.217.138.142,52.80.164.254,52.37.121.23,54.201.141.254,54.191.198.208,54.213.128.71,34.201.43.41,54.83.17.159,18.236.203.158,34.223.174.176,54.227.125.91,54.244.45.0,54.190.200.245,54.202.159.247,54.202.89.49,18.237.101.170,35.80.148.215,34.203.31.197,54.201.177.141,18.212.34.251,54.175.155.50,18.236.135.156,54.202.217.189,3.94.80.148,35.175.242.133,34.216.177.184,3.124.249.19,3.88.101.149,52.80.122.191,54.152.101.174,54.243.0.218,54.224.101.176,3.86.139.232,18.208.169.101,47.251.47.205,54.148.142.197,34.217.24.39,52.81.41.182,54.244.42.199,47.251.43.165,54.159.137.73,52.26.181.94,54.190.67.118,18.236.115.234,184.73.130.71,54.203.56.57,54.223.156.33,35.173.242.56,47.251.48.76,3.83.135.7,18.237.131.0,52.89.109.237,54.223.253.81,52.33.147.217,100.26.146.22,100.26.100.221,34.221.244.177,34.222.155.203,54.186.114.42,52.26.230.13,35.163.244.183,54.184.77.217,54.69.123.228,3.91.31.25,34.209.212.131,34.219.160.132,54.90.122.43,3.90.163.116,3.84.181.242,54.185.38.186,52.89.243.94,52.32.236.98,54.222.183.149,54.186.183.235,35.161.60.166,18.236.136.9,54.162.128.45,52.39.173.28,35.173.128.241,35.162.10.231,34.213.137.124,54.161.230.108,3.121.232.161,52.81.219.186,54.202.82.129,54.202.148.46,54.88.113.174,54.188.54.101,18.208.191.232,34.218.235.193,52.43.23.190,34.219.164.87,34.217.120.222,52.81.225.173,52.35.96.114,54.185.228.19,54.226.83.20,54.167.6.232,52.87.177.232,52.81.183.158,52.81.98.176,54.173.125.34,54.186.31.105,54.191.215.67,54.191.83.74,52.32.123.22,34.215.21.175,54.226.61.133,34.211.186.81,54.189.22.246,3.87.23.58,34.219.64.150,18.208.223.120,18.236.79.159,54.84.11.134,54.185.191.139,54.209.82.172,18.237.132.19,54.218.132.35,34.222.136.192,54.222.240.247,34.219.59.189,52.42.162.242,52.33.235.239,3.82.221.18,54.174.91.127,54.245.190.141,35.174.136.195,52.42.190.128,100.26.195.12,52.91.201.89,54.165.14.154,54.186.88.90,34.214.25.44,34.217.106.78,34.212.233.155,52.207.244.141,52.32.150.110,54.148.46.101,52.39.102.181,3.93.212.147,34.209.36.45,3.87.213.93,54.162.25.102,18.236.90.66,52.24.74.142,52.23.194.112,54.186.90.135,3.95.223.188,52.27.66.244,3.84.13.87,3.80.129.221,54.211.18.176,54.85.139.150,3.82.176.162,3.82.116.0,34.216.97.57,34.207.88.86,54.175.190.228,54.188.107.40,54.214.194.140,184.73.150.177,52.41.121.229,34.215.196.108,34.230.72.241,34.221.222.51,34.221.215.215,34.221.10.176,3.89.9.26,54.244.135.3,34.220.62.161,54.210.207.104,54.213.196.19,35.161.203.135,35.163.133.138,54.197.66.148,52.35.238.144,18.193.7.155,35.167.31.135,18.205.185.98,3.125.23.233,52.58.187.102,34.215.245.163,54.213.193.215,34.212.59.170,3.92.179.178,54.186.32.36,54.184.179.240,52.81.97.69,52.36.143.247,3.87.220.197,54.191.79.169,54.212.92.48,18.212.179.29,52.80.216.255,18.208.145.14,3.88.28.41,3.92.132.66,52.26.80.189,54.146.180.93,52.81.100.235,34.221.220.16,3.89.8.214,3.92.187.174,35.81.151.94,18.212.35.111,34.213.11.217,54.149.7.137,54.184.85.102,54.152.85.45,3.95.221.218,54.174.118.195,3.83.144.79,34.207.164.222,35.163.238.252,34.227.177.91,52.88.139.16,18.237.107.123,3.87.195.81,54.214.207.234,34.222.244.9,34.220.166.165,54.189.134.38,34.205.140.79,34.212.151.207,52.80.126.53,52.33.37.221,54.172.32.111,52.81.222.160,54.212.52.251,18.215.150.26,34.222.242.100,54.218.124.97,3.93.242.88,54.223.211.149,34.222.218.62,3.88.187.44,35.160.25.211,35.162.179.169,34.218.208.24,34.209.143.77,52.42.101.26,35.166.223.105,52.87.151.254,54.188.6.177,54.91.193.60,34.221.143.124,54.187.155.85,54.191.143.45,34.219.55.27,52.91.159.219,34.204.0.114,54.190.191.172,34.221.48.69,44.239.125.26,18.207.127.58,34.209.175.3,18.237.65.16,54.190.67.106,52.24.92.110,34.219.8.218,35.81.134.48,34.220.191.87,52.41.208.39,35.155.235.211,54.173.129.84,54.201.40.14,34.220.134.92,52.32.51.134,34.230.10.18,34.221.44.205,54.201.174.65,54.214.214.187,34.209.107.61,54.188.172.101,52.80.105.102,54.189.173.68,34.210.105.253,52.4.21.49,34.223.249.124,34.221.4.173,34.221.72.157,54.187.158.179,54.185.124.10,34.219.142.24,54.184.217.108,34.222.167.71,34.215.218.1,54.244.39.187,52.43.143.2,3.95.174.226,52.32.225.91,54.212.232.119,35.163.124.104,52.23.228.90,52.12.252.138,34.212.167.147,54.187.143.237,54.191.52.179,54.187.38.214,52.36.74.143,54.191.124.83,52.40.124.148,52.88.238.15,35.163.134.112,54.190.3.47,52.32.48.129,34.211.50.37,52.24.203.216,52.42.120.220,54.201.176.65,54.202.107.52,34.214.73.225,34.220.134.210,34.211.113.100,34.220.182.44,54.245.52.111,34.208.219.135,54.159.13.70,18.237.238.185,54.187.151.1,54.237.12.198,54.196.232.34,54.202.54.228,34.222.167.162,34.230.47.65,52.87.250.187,52.34.139.9,34.217.114.207,54.222.188.27,18.212.228.207,34.231.253.8,52.25.64.105,52.26.85.179,35.172.222.146,52.81.205.10,52.11.230.243,54.159.60.120,52.12.170.121,54.189.135.9,18.237.94.16,34.217.146.127,34.219.9.33,54.189.19.187,54.223.105.144,54.213.229.207,54.245.185.217,35.161.43.210,3.85.93.16,54.201.254.156,54.223.137.124,35.166.51.201,52.43.83.62,34.219.170.222,54.213.172.119,34.217.177.38,54.184.254.236,54.212.153.8,34.213.155.60,34.210.142.115,54.202.193.35,18.237.9.8,34.214.97.118,54.189.159.45,52.80.124.255,52.27.28.17,18.236.82.249,34.216.205.141,34.222.132.199,34.208.20.69,52.33.20.16,34.211.55.139,54.201.9.48,54.222.134.167,52.81.34.202,34.221.245.41,18.215.152.186,52.36.50.3,34.211.48.217,52.81.15.235,54.223.96.242,34.221.243.69,52.80.140.147,35.167.197.155,35.162.20.60,54.244.155.180,52.80.3.144,35.165.137.10,54.149.226.124,18.237.151.229,3.87.125.184,54.190.49.164,34.217.11.127,34.211.20.186,34.218.249.124,54.190.79.140,18.237.123.245,39.96.45.146,54.70.115.216,52.33.250.20,54.201.206.144,52.81.50.70,52.40.232.46,52.81.28.214,34.220.211.8,52.80.25.25,54.82.125.88,170.106.199.193,52.81.45.149,123.56.44.251,52.80.121.203,3.86.162.127,52.81.32.218,52.81.34.45,52.80.170.48,52.81.105.34,3.83.86.138,43.130.38.66,54.244.105.62,54.188.180.199,54.202.13.228,170.106.197.235,34.217.110.81,170.106.198.80,170.106.199.148,52.90.238.209,35.161.165.22,52.32.93.159,140.179.50.0,44.234.190.117,44.227.242.10,170.106.174.211,35.167.176.59,52.41.62.177,35.167.162.139,52.12.153.20,34.211.145.4,35.165.147.60,52.40.195.175,34.214.82.235,54.149.236.240,34.212.54.241,35.80.38.82,34.235.130.204,18.236.95.153,34.219.135.208,34.213.78.27,34.219.178.138,54.191.18.148,34.217.97.42,52.23.188.102,54.144.216.248,34.210.6.2,54.188.70.222,54.161.41.148,54.190.59.88,35.162.177.144,54.202.99.175,34.216.104.224,52.26.46.34,52.81.191.224,52.27.103.196,34.222.33.131,54.203.74.139,52.37.186.225,34.209.215.13,35.165.137.140,54.70.178.130,54.186.128.190,35.167.54.246,52.39.53.97,52.43.36.43,34.221.147.47,18.236.183.129,34.220.229.53,54.148.143.123,54.187.209.56,35.160.221.140,18.236.101.255,54.191.67.245,35.166.182.251,54.244.203.120,52.80.95.169,52.42.174.171,54.185.200.180,52.40.156.141,54.218.230.46,54.184.90.250,82.156.168.80,52.27.183.221,82.157.53.196,82.157.57.197,34.220.105.136,54.189.140.104,52.38.100.80,34.216.167.159,34.214.184.185,34.210.171.30,54.71.56.210,34.209.194.112,54.71.117.247,18.237.75.184,184.72.75.53,34.217.42.188,52.11.60.247,100.26.41.111,34.210.64.188,100.21.18.212,54.235.48.141,44.242.154.130,34.208.130.201,54.235.2.103,18.236.155.40,35.165.66.188,34.213.108.216,34.222.146.145,44.234.88.63,54.213.146.214,34.213.189.236,54.160.199.68,3.82.21.233,35.161.147.223,34.212.63.222,35.160.198.113,34.216.227.175,54.188.67.7,54.213.7.9,18.212.239.63,18.236.116.170,34.214.103.184,54.202.55.236,52.35.23.52,34.221.189.62,34.219.225.156,3.86.60.227,34.210.78.218,34.218.232.194,54.188.187.154,54.202.89.241,34.222.243.73,54.187.83.12,52.80.209.211,52.81.90.73,34.214.150.32,34.230.78.232,100.27.28.196,52.90.29.79,184.72.203.3,54.203.14.113,54.212.202.93,52.33.76.185,54.223.176.211,34.209.140.156,52.34.132.3,52.33.235.57,34.220.149.118,54.245.162.95,100.26.21.54,52.26.198.66,34.220.217.39,35.162.228.240,34.221.18.90,34.222.91.199,54.203.16.61,52.42.205.223,52.39.146.113,34.220.78.25,34.219.250.182,34.210.4.210,54.244.85.3,3.86.145.132,35.163.8.142,34.219.123.183,34.217.34.231,34.220.43.127,34.219.207.174,35.164.131.186,54.245.10.33,34.220.71.10,54.245.57.144,54.202.36.63,34.220.132.116,18.207.215.230,52.41.81.30,47.254.32.197,52.43.201.225,54.244.42.10,54.202.68.44,54.149.138.41,47.251.52.99,34.217.65.84,47.251.53.179,34.210.192.193,52.25.209.39,47.251.54.156,52.37.147.21,52.33.70.130,52.89.35.49,54.69.230.16,52.11.4.104,52.12.122.106,47.251.17.77,54.218.230.248,34.222.27.21,47.251.20.129,34.210.174.209,52.33.23.116,34.216.12.33,34.220.6.187,52.24.58.242,35.163.137.125,52.11.116.194,34.210.8.23,52.24.140.195,54.186.113.215,52.37.54.65,54.68.182.39,34.221.203.68,52.40.21.171,52.25.75.21,34.220.75.238,54.200.133.166,54.213.255.55,52.33.123.163,34.221.206.7,35.162.188.167,35.164.136.59,35.164.197.223,35.164.181.78,54.70.228.45,52.40.148.28,54.245.58.101,52.205.255.211,34.212.35.19,54.70.167.183,52.33.171.229,52.25.132.119,52.24.20.119,52.24.109.189,52.40.157.163,34.221.106.147,52.40.50.221,35.164.64.93,52.32.62.127,54.68.202.255,52.29.100.114,52.42.163.196,35.162.80.135,52.42.146.137,35.163.47.72,54.149.3.59,18.197.75.9,34.210.57.53,34.82.81.91,54.211.233.228,34.224.80.242,34.226.193.235,34.222.85.126,34.222.33.230,54.188.254.88,35.233.191.130,35.164.0.102,34.215.179.90,54.218.202.163,18.236.137.185,52.81.28.118,54.184.103.187,54.69.177.154,34.220.33.176,54.186.72.190,54.209.3.61,54.184.198.87,34.221.74.103,35.167.93.223,34.217.42.21,52.81.101.58,3.89.225.253,52.13.122.116,18.236.93.233,52.81.69.29,18.236.173.46,34.213.178.236,3.90.102.114,54.202.33.237,54.89.71.238,100.20.87.229,54.213.131.193,34.212.75.112,34.220.1.77,54.245.164.107,54.148.3.125,35.161.180.140,54.187.44.140,18.236.202.228,35.160.156.81,54.90.151.89,35.197.22.140,52.34.96.27,35.80.15.38,54.69.155.162,54.148.235.37,18.237.155.96,34.237.149.96,54.209.161.124,34.213.51.247,35.163.88.131,3.89.106.230,34.211.151.74,18.237.127.209,54.88.122.46,34.223.253.154,34.222.7.186,34.213.227.209,54.212.77.83,54.149.214.223,34.196.205.201,34.208.60.32,54.70.84.88,34.233.94.197,54.71.88.25,54.187.247.82,39.97.127.87,18.236.174.144,52.37.166.174,52.81.191.161,34.221.38.224,35.164.179.224,52.41.212.122,34.233.128.56,35.167.65.164,54.202.228.223,3.91.48.226,44.233.17.240,54.146.219.7,54.185.217.208,54.174.220.54,54.160.202.2,52.36.32.12,35.83.175.139,54.187.95.191,52.91.27.219,54.184.247.151,54.188.79.208,34.219.79.35,3.91.216.216,54.214.218.161,34.205.218.212,34.215.227.157,52.81.165.38,54.218.133.178,54.201.159.92,52.13.93.189,34.222.0.10,54.148.138.79,52.13.110.59,54.244.161.198,52.32.166.167,54.149.171.3,52.80.65.39,54.212.163.142,34.220.129.129,54.68.22.83,34.239.37.80,3.87.7.14,18.209.19.102,35.161.221.218,54.164.58.14,34.217.72.252,47.254.28.177,52.80.125.97,54.223.169.43,44.225.253.232,18.237.197.145,54.222.131.27,34.212.229.156,44.242.142.221,52.37.226.140,34.208.70.113,35.167.168.23,34.217.137.171,54.226.79.96,18.237.211.56,54.223.186.84,54.223.174.120,34.213.38.190,54.212.188.171,54.213.178.196,52.81.24.133,54.245.63.122,35.163.44.34,34.216.55.194,35.82.33.149,34.212.187.93,18.237.226.60,54.68.155.45,35.82.63.95,35.81.169.110,34.221.187.222,3.85.126.110,54.191.63.9,52.33.15.221,3.91.5.144,54.223.77.46,54.185.30.239,35.166.40.229,52.26.174.230,54.70.52.45,3.93.164.86,54.89.192.64,54.91.140.76,34.235.87.151,3.90.108.229,54.190.16.187,34.218.131.212,34.214.165.67,54.189.122.39,54.175.245.88,52.80.215.222,3.93.194.164,52.37.128.231,54.200.192.112,54.208.11.67,54.212.209.89,44.230.66.106,52.201.84.229,54.68.89.161,34.215.202.46,18.236.153.148,18.236.141.132,34.220.151.148,34.203.199.60,18.237.103.191,35.164.198.39,54.187.6.210,3.83.51.227,52.80.163.132,54.208.234.34,54.69.164.6,35.166.4.249,34.223.214.16,47.251.21.39,52.42.31.162,54.164.183.5,35.165.237.35,52.36.68.90,52.80.168.89,34.211.15.18,52.12.125.193,54.147.244.203,34.238.211.173,34.214.104.90,52.80.109.96,18.236.251.90,34.217.36.100,34.211.82.231,52.91.161.161,52.81.154.163,54.226.9.74,54.186.182.173,3.95.39.225,54.71.203.221,52.43.243.127,52.23.197.209,52.80.36.156,52.83.210.47,34.211.228.132,34.221.3.11,54.148.103.82,54.213.202.14,18.212.100.46,3.80.36.235,52.80.119.4,52.38.223.86,52.32.58.131,54.234.32.4,54.68.95.133,54.209.233.123,34.204.51.160,54.221.130.143,34.210.182.230,54.91.136.44,34.222.37.165,18.236.235.163,54.202.129.107,34.221.16.64,34.213.186.105,3.95.9.2,34.216.252.97,54.190.190.255,52.80.182.140,54.225.3.210,34.217.113.146,54.243.27.7,52.80.113.126,34.222.91.97,34.215.151.120,34.219.186.116,34.220.227.177,47.93.10.172,54.201.136.88,52.81.22.116,52.81.63.36,34.214.250.198,34.221.148.39,54.69.175.29,35.162.1.34,18.236.145.10,52.10.64.188,52.12.110.90,18.237.100.221,54.184.6.116,52.80.11.1,52.81.69.55,54.172.14.194,18.237.144.148,34.203.34.117,54.88.83.237,52.91.192.180,18.215.155.195,54.245.40.213,54.223.249.103,18.236.187.9,54.223.125.159,52.80.37.14,34.228.24.227,35.160.97.111,52.37.224.59,52.88.4.84,34.212.80.183,34.221.161.220,54.149.169.243,18.237.61.3,52.88.177.251,34.217.74.188,34.222.155.27,34.220.255.174,18.234.196.68,3.127.184.129,44.229.244.29,34.209.238.83,54.164.230.193,54.91.170.145,54.245.29.168,52.81.73.250,54.222.181.56,34.221.206.22,54.223.126.95,54.152.104.182,54.87.39.43,3.90.221.161,54.210.167.57,54.245.185.15,34.214.236.194,34.221.24.37,18.236.6.210,34.222.56.203,3.90.3.4,54.163.45.41,3.95.56.65,100.20.20.15,54.202.40.196,44.242.153.49,52.32.118.219,44.235.91.217,3.89.56.194,54.147.144.233,35.227.187.197,54.212.241.233,3.83.142.165,54.68.163.23,54.211.220.142,34.220.210.77,54.198.152.246,54.91.209.164,35.164.16.245,3.81.230.163,54.186.164.183,52.80.40.176,54.160.211.183,34.216.251.32,34.210.66.237,34.203.35.65,54.174.185.103,34.209.7.53,18.236.201.67,34.216.70.36,34.229.80.9,34.209.151.227,54.85.105.146,54.202.136.23,54.209.50.172,52.32.57.114,34.211.133.141,54.202.109.39,52.26.93.14,54.234.57.191,107.21.137.157,34.220.237.3,54.165.36.117,54.187.229.57,34.217.46.47,18.236.178.201,34.208.185.90,54.91.86.116,54.234.244.10,34.219.8.64,54.213.68.113,54.82.90.172,82.156.164.49,18.237.50.29,34.221.118.61,107.20.130.77,52.12.19.128,35.162.133.138,52.12.245.40,18.237.127.64,3.84.236.249,3.93.239.130,54.149.194.142,35.166.245.111,34.212.134.146,34.223.6.170,34.209.204.56,3.90.200.156,34.222.51.254,52.73.236.212,54.213.89.176,18.236.163.237,34.212.227.125,18.237.53.227,52.10.80.205,54.245.151.53,44.232.160.206,18.206.251.159,44.242.152.210,3.86.183.190,34.221.50.84,3.87.77.156,54.212.144.53,35.166.17.223,3.82.152.171,34.221.78.57,39.97.109.32,54.148.74.247,52.37.94.192,34.221.237.55,54.91.249.28,54.149.79.247,54.191.61.195,54.149.43.15,52.10.34.121,34.221.187.199,52.40.187.46,18.237.202.98,34.216.169.73,35.164.13.230,34.216.213.184,54.218.111.117,54.185.66.29,35.174.166.195,35.164.148.133,35.161.180.9,52.88.195.133,34.208.94.223,34.220.129.121,3.83.155.130,34.214.88.8,34.221.85.240,34.222.110.188,52.32.68.74,54.158.240.62,35.161.177.171,44.234.105.36,54.166.247.231,18.236.185.168,54.164.143.16,52.55.180.81,54.186.184.248,3.214.197.216,34.220.143.44,54.148.172.58,34.226.122.112,52.81.48.196,34.215.75.153,34.212.30.80,3.83.87.254,54.244.156.227,54.175.99.187,34.220.197.127,52.38.233.81,34.211.225.58,44.226.204.96,52.81.230.48,54.186.163.171,54.187.212.182,52.0.206.97,54.245.6.39,52.43.158.228,34.208.227.183,100.20.122.142,52.90.161.91,52.42.180.35,35.82.16.133,35.162.57.3,184.73.21.220,54.90.247.128,54.162.155.23,52.36.255.16,34.238.154.32,8.131.238.210,34.204.14.38,52.26.164.231,35.80.17.156,52.80.117.52,54.167.6.199,34.222.232.160,34.217.133.47,18.157.77.235,107.23.135.81,52.81.49.74,34.227.75.45,54.227.42.223,34.211.110.33,35.155.216.238,54.226.67.31,54.213.124.255,3.95.25.26,54.200.217.125,52.10.5.125,54.148.186.193,54.222.155.153,34.235.139.157,52.80.95.120,52.80.193.153,52.80.178.123,54.202.201.191,54.210.8.91,54.203.38.58,54.191.157.0,34.222.12.148,44.228.97.75,3.82.192.40,34.221.49.18,54.69.39.145,39.96.5.114,54.189.135.224,52.81.95.101,54.191.158.10,34.212.228.230,44.238.212.51,34.219.164.126,54.89.108.246,52.80.131.76,54.190.8.21,3.91.255.112,34.213.224.114,52.81.210.242,52.42.162.219,35.162.181.156,52.80.35.231,34.217.110.240,52.81.90.67,54.161.216.222,35.167.58.2,34.221.255.18,34.221.207.112,54.185.50.238,34.213.39.108,52.34.203.65,54.187.232.194,52.39.236.189,52.12.171.145,34.217.113.244,34.224.65.76,54.145.4.104,34.209.235.17,52.43.37.244,54.188.82.79,18.237.71.246,34.208.7.89,3.88.183.182,35.165.142.88,18.236.184.28,18.237.177.187,18.205.239.15,54.190.199.10,52.37.124.6,44.241.219.168,54.191.53.23,54.191.56.174,52.81.12.143,52.41.62.107,35.162.44.11,54.191.25.182,54.209.148.138,52.91.54.16,34.218.224.156,34.222.155.66,54.223.22.166,54.149.48.78,54.69.24.196,54.175.121.176,54.202.84.111,3.88.172.60,44.234.122.202,34.220.147.62,54.198.225.212,52.80.63.26,34.217.58.122,18.236.169.113,34.222.197.234,54.146.245.182,35.83.92.103,34.219.54.88,34.221.222.107,35.164.144.18,54.191.33.17,100.21.127.213,52.90.221.174,34.220.141.234,54.187.90.146,35.165.68.86,35.167.228.58,52.80.108.102,34.222.27.33,54.191.124.11,52.12.193.32,34.220.8.175,52.80.171.177,18.236.247.151,54.189.98.12,52.25.191.109,54.218.233.118,50.112.42.86,34.220.63.144,54.188.74.114,35.166.45.187,47.95.38.5,34.219.115.15,54.223.157.153,3.90.68.51,54.85.18.147,107.23.134.12,54.222.210.187,35.168.24.76,54.187.172.210,18.237.185.59,34.221.187.63,54.218.14.23,52.43.206.208,52.81.86.253,34.217.28.213,52.88.219.27,54.190.12.246,35.164.132.142,54.223.47.28,35.165.195.178,34.212.139.188,3.82.116.29,18.237.22.204,54.218.72.130,34.220.204.94,34.210.115.163,18.237.107.136,54.149.102.74,18.234.234.242,52.37.85.7,18.237.105.91,34.211.112.193,35.162.178.175,52.83.139.236,35.161.165.49,34.216.177.227,34.239.247.154,34.215.245.252,34.213.231.163,34.217.126.71,107.23.78.104,34.211.232.183,34.219.45.199,34.210.161.185,34.214.254.11,52.81.224.7,54.188.118.107,54.190.53.173,34.217.146.193,18.237.76.205,54.201.148.47,52.82.35.238,184.72.208.250,52.42.91.173,54.188.62.96,100.24.26.149,34.220.116.77,52.80.71.243,54.212.207.63,44.234.58.40,52.12.161.145,34.215.248.1,34.221.35.43,35.165.24.72,54.191.42.101,35.165.224.92,34.219.86.8,34.209.156.234,54.68.74.136,54.84.30.122,3.94.196.88,34.219.115.148,54.218.60.138,54.88.97.56,34.210.9.149,54.223.114.238,18.237.130.207,54.226.96.108,35.163.203.119,54.223.193.80,52.22.73.219,34.223.146.117,44.225.116.6,34.212.210.94,34.213.8.136,52.36.254.187,52.32.243.246,54.222.168.252,18.205.163.121,34.219.118.52,34.221.67.37,3.86.208.182,52.34.236.254,54.245.130.31,35.167.89.83,54.175.200.124,54.89.159.24,54.200.85.39,35.167.183.39,54.190.160.237,54.172.220.118,34.212.110.14,35.166.8.242,34.213.21.140,35.172.190.68,34.213.225.11,54.226.248.45,34.210.69.36,18.236.193.255,34.208.66.169,52.43.145.46,34.210.81.133,3.90.161.185,52.42.196.22,18.211.102.211,52.86.199.199,34.209.164.68,52.6.228.115,39.97.124.70,54.234.102.31,54.191.14.84,54.191.205.125,54.212.105.100,52.10.109.31,34.220.52.217,54.202.218.22,18.206.175.20,54.198.224.234,34.235.129.123,54.221.28.90,54.71.0.2,54.90.104.41,54.222.133.241,54.145.14.80,54.235.1.232,52.204.130.115,34.236.151.192,52.32.144.198,54.209.199.63,54.223.157.25,54.162.141.197,18.236.153.151,52.80.127.241,52.80.122.244,34.219.203.134,54.223.122.134,54.149.111.118,18.208.139.75,18.212.231.124,54.242.220.224,52.81.64.75,34.215.96.161,34.219.168.42,34.209.250.41,3.80.151.224,18.212.92.144,52.32.164.141,34.219.34.121,47.93.25.45,18.237.145.121,18.236.218.235,3.84.197.6,54.189.23.192,34.228.64.247,54.223.82.176,52.81.22.137,54.212.27.55,34.221.117.108,39.106.182.201,18.236.83.34,3.82.214.132,52.12.10.21,34.221.98.127,52.80.64.8,52.80.13.153,34.219.36.74,52.80.208.76,52.81.58.209,34.220.216.243,34.209.58.223,35.164.159.45,52.37.112.232,54.223.109.109,44.229.69.42,34.215.216.101,52.32.136.163,52.81.210.30,54.188.70.173,52.27.144.202,52.81.30.109,52.81.40.227,18.236.135.159,52.81.65.126,52.80.83.54,18.236.210.102,54.211.64.59,18.236.225.176,54.223.184.162,54.190.118.204,18.215.152.154,18.236.159.15,54.203.147.94,52.81.101.154,34.209.206.188,54.185.251.240,35.164.185.223,54.187.64.121,69.230.211.230,54.212.244.28,34.209.65.79,52.54.222.7,52.207.118.100,34.222.20.168,18.236.175.24,34.220.159.236,54.166.90.39,54.200.73.33,34.220.123.13,35.165.111.61,54.190.65.166,35.162.61.242,34.222.1.255,18.236.226.83,54.223.183.173,3.84.54.6,34.201.64.166,52.36.145.23,54.90.232.218,54.163.59.108,100.24.68.170,3.95.211.64,3.80.76.47,54.91.231.32,54.213.75.165,34.229.215.190,54.188.67.135,44.226.207.56,52.54.215.77,52.37.11.16,34.229.140.57,54.92.193.120,34.213.229.137,35.163.90.151,54.214.176.39,54.186.135.245,35.162.18.39,52.36.111.38,34.211.227.5,54.174.239.1,3.85.103.101,18.206.211.2,35.169.44.210,54.245.150.136,54.190.235.229,54.186.7.220,34.220.203.234,54.218.81.162,18.236.85.57,35.160.34.194,54.185.13.121,54.187.190.69,34.217.211.221,35.166.126.69,54.223.176.155,34.209.154.192,54.226.255.221,18.237.107.254,35.165.115.156,52.42.210.149,18.209.65.52,54.71.198.245,54.214.56.79,18.236.186.232,3.80.222.39,184.72.154.27,52.35.143.155,34.204.42.43,18.212.247.33,18.236.190.24,34.224.82.8,52.81.29.192,18.236.86.3,34.222.16.232,54.218.220.168,54.223.93.8,34.222.128.60,54.144.121.229,54.184.55.206,52.13.219.53,34.218.74.85,54.186.82.194,18.237.198.107,52.89.255.35,54.214.109.65,34.219.194.245,54.223.251.66,54.213.22.88,54.191.130.100,44.233.150.26,52.36.222.152,34.208.92.196,34.221.31.213,35.163.121.236,34.218.80.140,54.223.205.23,52.13.57.155,54.190.208.107,54.209.249.72,54.174.66.96,54.202.130.144,52.88.52.166,52.12.180.19,34.223.239.215,54.189.132.176,18.184.168.181,44.233.150.254,54.201.225.116,170.106.189.91,34.220.155.131,34.221.68.136,54.163.162.12,54.218.170.69,54.202.244.33,54.173.152.231,18.237.192.97,44.237.161.130,52.81.21.35,54.223.213.188,3.83.54.116,44.233.126.227,35.160.207.123,54.148.137.186,3.219.221.64,35.167.225.140,54.156.254.254,54.201.1.52,54.68.253.182,34.213.156.193,54.222.131.78,34.212.38.150,54.223.191.47,54.167.101.36,34.209.219.31,54.244.199.3,34.215.10.87,34.216.52.243,34.219.42.47,54.71.91.36,52.13.84.75,54.222.142.143,34.212.225.44,54.189.225.85,52.81.152.214,54.245.32.90,3.83.235.249,54.200.93.132,54.196.19.206,52.43.243.181,34.211.235.198,34.220.119.176,18.237.52.200,18.236.169.74,18.237.212.111,34.221.171.106,54.147.9.46,52.12.155.75,34.238.44.211,52.81.90.236,54.167.8.15,44.238.141.221,52.81.205.203,18.237.246.211,54.185.94.138,54.222.177.164,54.202.170.112,3.92.79.16,54.208.129.232,52.88.50.64,18.212.175.249,34.214.122.24,34.219.59.187,34.220.101.252,54.70.60.71,50.112.37.207,54.68.243.34,34.212.37.107,52.81.253.10,54.223.163.228,34.213.60.244,34.213.31.246,54.157.164.121,54.71.185.93,54.227.41.204,52.40.98.232,18.234.241.171,52.91.40.176,184.73.11.79,54.212.210.59,34.230.20.196,34.219.176.247,44.242.140.141,34.211.231.82,34.216.220.30,34.209.141.6,23.23.66.107,3.209.147.110,34.208.216.91,3.92.217.87,54.70.213.99,34.216.212.186,54.83.184.161,34.209.27.165,34.239.246.158,34.216.159.159,54.86.89.220,34.223.253.212,54.234.160.96,54.175.45.135,54.205.205.177,54.91.79.238,34.223.100.11,34.219.78.111,44.228.194.84,34.222.134.214,52.91.216.54,54.212.91.169,3.80.25.168,18.237.127.72,34.222.213.105,54.210.235.40,35.164.157.62,18.207.94.43,34.222.69.220,34.197.31.27,54.223.51.114,54.145.29.16,52.91.131.15,34.219.127.81,18.237.210.209,52.41.119.219,3.87.75.252,35.160.17.225,35.164.4.252,34.223.201.185,52.12.232.13,54.189.66.189,54.214.119.201,52.80.58.202,54.158.27.249,18.236.219.56,3.88.91.46,54.89.192.151,52.80.4.38,34.211.150.12,34.230.61.29,34.217.55.104,34.213.95.222,54.202.61.159,34.219.113.90,34.211.150.207,34.209.234.176,54.157.15.131,54.146.190.202,52.43.169.216,34.222.242.250,44.242.148.177,35.165.106.49,34.207.150.93,52.32.97.24,54.175.15.206,54.190.158.133,34.215.28.36,54.201.171.159,44.226.198.230,54.223.149.57,52.81.186.100,34.215.71.82,18.237.155.32,100.21.17.191,44.234.60.100,54.196.178.103,35.155.69.199,52.80.50.153,35.174.167.98,44.230.198.114,34.231.208.16,54.222.171.244,18.192.197.160,54.190.87.158,34.219.24.224,52.23.199.169,3.122.178.154,44.233.150.252,34.208.4.159,34.216.94.27,54.82.237.85,34.206.76.137,54.244.162.132,35.81.164.199,18.236.231.243,52.81.79.129,39.107.231.24,50.112.49.161,52.81.32.215,18.215.165.100,54.223.130.203,35.166.237.230,54.223.247.19,54.164.48.203,34.211.183.232,54.222.212.180,34.220.39.29,18.236.63.7,54.152.203.186,50.112.15.167,54.200.189.77,44.235.254.89,52.55.254.254,3.83.137.23,35.80.31.88,44.228.129.122,52.12.5.64,54.187.178.65,44.242.155.108,52.81.42.171,54.222.166.73,54.173.17.72,54.222.254.75,52.81.198.216,8.140.10.9,52.80.254.48,54.236.21.32,52.80.1.69,44.234.104.149,34.216.219.121,35.162.67.160,52.72.221.227,54.222.254.165,44.234.45.88,44.228.229.10,44.226.206.6,54.201.246.180,54.212.1.235,52.80.101.248,35.161.96.100,52.11.182.235,52.81.94.58,54.191.4.162,54.212.115.223,18.234.87.80,54.222.215.33,54.87.29.50,34.203.10.72,44.232.132.132,34.219.185.128,54.87.205.7,34.227.94.171,34.217.206.52,54.234.48.55,54.91.218.12,54.149.166.80,54.175.12.143,100.26.234.182,54.202.249.81,54.222.244.210,54.223.238.99,54.203.218.199,54.89.191.164,34.212.162.187,54.223.134.4,8.131.88.151,3.87.124.119,34.228.142.130,52.81.196.128,54.226.16.244,34.227.78.140,34.228.186.148,52.81.191.193,52.27.196.21,52.80.94.98,52.81.93.173,54.163.43.73,54.223.94.222,54.203.132.202,3.66.191.192,52.89.72.105,52.80.68.150,52.80.187.254,34.219.122.126,52.81.32.160,52.80.203.9,52.80.60.198,54.222.152.80,54.222.226.49,52.80.246.168,52.80.44.48,34.210.21.69,52.81.231.56,52.81.97.171,52.81.32.71,52.80.116.137,52.81.229.217,54.226.55.36,100.21.157.81,52.40.226.38,52.26.138.227,54.223.136.23,34.210.40.177,34.219.222.78,52.33.120.37,3.93.56.30,52.91.18.89,3.94.163.162,34.217.179.37,34.201.43.154,100.27.5.86,34.207.115.7,34.224.37.99,3.90.16.180,54.146.102.220,35.167.240.135,54.89.170.135,54.242.221.60,34.229.205.254,34.227.46.154,54.226.44.110,54.172.207.19,52.91.244.196,54.159.42.68,3.83.229.210,34.222.12.203,54.242.64.44,34.210.134.4,34.218.246.29,34.216.97.115,52.89.98.16,52.33.40.245,174.129.45.185,35.161.50.187,100.24.36.31,52.80.189.78,44.234.44.66,54.149.179.176,54.202.240.69,34.209.231.212,54.223.31.182,54.146.232.8,54.71.83.100,52.90.148.175,100.25.216.240,34.217.38.53,52.80.27.25,3.81.224.182,39.106.196.131,34.222.120.199,34.222.175.69,52.36.150.53,54.174.217.222,18.209.7.243,54.149.66.2,34.214.130.155,18.237.81.223,34.215.163.54,34.222.116.98,52.37.126.72,34.219.218.142,34.221.139.118";
        $portStr = "21,22,23,25,53,80,81,110,111,123,135,137,139,161,389,443,445,465,500,515,520,523,548,623,636,873,902,1080,1099,1433,1521,1604,1645,1701,1883,1900,2049,2181,2375,2379,2425,3128,3306,3389,4730,5060,5222,5351,5353,5432,5555,5601,5672,5683,5900,5938,5984,6000,6379,7001,7077,8080,8081,8443,8545,8686,9000,9001,9042,9092,9100,9200,9418,9999,11211,27017,37777,50000,50070,61616";

        $cmd = "masscan --ports {$portStr} {$ip}  --max-rate 50000  --wait  5 |grep Discovered";

        echo $cmd;

        execLog($cmd, $result);

        file_put_contents("11.txt", json_encode($result));
        $result = json_decode(file_get_contents("11.txt"), true);

        foreach ($result as $value) {
            $aaa = explode(" ", $value);

            $typeArr = explode("/", $aaa[3]);
            $data = ['host' => $aaa[5], 'type' => $typeArr[1], 'port' => $typeArr[0]];

            if (empty(Db::table('host_port')->where($data)->find())) {
                Db::table('host_port')->save($data);
            }
        }


    }

    public static function scanHostPort()
    {
        $portStr = "21,22,23,25,53,80,81,110,111,123,135,137,139,161,389,443,445,465,500,515,520,523,548,623,636,873,902,1080,1099,1433,1521,1604,1645,1701,1883,1900,2049,2181,2375,2379,2425,3128,3306,3389,4730,5060,5222,5351,5353,5432,5555,5601,5672,5683,5900,5938,5984,6000,6379,7001,7077,8000,8001,8080,8081,8443,8545,8686,8888,9000,9001,9042,9092,9100,9200,9418,9999,11211,27017,37777,50000,50070,61616";
        while (true) {
            processSleep(1);
            $endTime = date('Y-m-d', time() - 86400 * 15);
            $hostLit = Db::table('host')->whereTime('port_scan_time', '<=', $endTime)->limit(5)->orderRand()->select()->toArray();
            foreach ($hostLit as $val) {
                PluginModel::addScanLog($val['id'], __METHOD__, 0,1);
                $host = gethostbyname($val['host']);

                $cmd = "masscan --ports {$portStr} {$host}  --max-rate 2000 |grep Discovered";

                execLog($cmd, $result);

                foreach ($result as $value) {
                    $aaa = explode(" ", $value);

                    $typeArr = explode("/", $aaa[3]);
                    $data = ['host' => $aaa[5], 'type' => $typeArr[1], 'port' => $typeArr[0],'user_id'=>$val['user_id'],'app_id'=>$val['app_id']];
                    addlog(["发现主机开放端口", $data]);
                    Db::table('host_port')->extra("IGNORE")->insert($data);

                }

                Db::table('host')->where(['id' => $val['id']])->save(['port_scan_time' => date('Y-m-d H:i:s')]);
                PluginModel::addScanLog($val['id'], __METHOD__, 1,1);
            }

            print_r("扫描主机端口完成，休息10秒...");
            sleep(10);
        }
    }

    // 更新ip区域以及运营商信息
    public static function upadteRegion()
    {
        while (true) {
            processSleep(1);
            $obj = Db::table('host')->whereTime('ip_scan_time', '<=', date('Y-m-d H:i:s', time() - (86400 * 15)));
            $list = $obj->where('is_delete', 0)->limit(10)->orderRand()->select()->toArray();

            foreach ($list as $v) {
                PluginModel::addScanLog($v['id'], __METHOD__, 0,1);
                $result = get_ip_lookup($v['host']);
                if (!isset($result['data'])) {
                    PluginModel::addScanLog($v['id'], __METHOD__, 2,1);
                    addlog(["未获取此IP{$v['host']}信息"]);
                    self::updateScanTime($v['id']);
                    continue;
                }

                $result = $result['data'];
                if ($result) {
                    $ids[] = $v['id'];

                    $v['country'] = isset($result['country']) ? $result['country'] : '';
                    $v['region'] = $result['region'];
                    $v['city'] = $result['city'];
                    $v['area'] = $result['area'];
                    $v['isp'] = $result['isp'];
                    $v['ip_scan_time'] = date('Y-m-d H:i:s', time());
                    Db::table('host')->save($v);
                }
                PluginModel::addScanLog($v['id'], __METHOD__, 1,1);
            }

            addlog(["更新IP信息完成，休息30秒..."]);
            sleep(30);
        }
    }

    public static function MasFullScan()
    {
        $portStr = "21,22,23,25,53,80,81,110,111,123,135,137,139,161,389,443,445,465,500,515,520,523,548,623,636,873,902,1080,1099,1433,1521,1604,1645,1701,1883,1900,2049,2181,2375,2379,2425,3128,3306,3389,4730,5060,5222,5351,5353,5432,5555,5601,5672,5683,5900,5938,5984,6000,6379,7001,7077,8080,8081,8443,8545,8686,9000,9001,9042,9092,9100,9200,9418,9999,11211,27017,37777,50000,50070,61616";

        $hostLit = Db::table('host')->select()->toArray();


//        $data = array_column($hostLit,'host');

        var_dump($hostLit);
        die;
        //扫描内部网段
//        $hostLit = Db::table('host_port')->whereLike("host", "10.%")->group('host')->select()->toArray();
//        $data = [];
//        foreach ($hostLit as $value) {
//            $tempArr = explode(".", $value['host']);
//
//            $key = "{$tempArr[0]}.{$tempArr[1]}.{$tempArr[2]}.0";
//
//            $data[$key] = 1;
//        }
//        $data = array_keys($data);

        foreach ($data as $val) {
            $cmd = "masscan --ports {$portStr} {$val}/24  --max-rate 2000 |grep Discovered";
            execLog($cmd, $result);

            foreach ($result as $value) {
                $aaa = explode(" ", $value);

                $typeArr = explode("/", $aaa[3]);
                $data = ['host' => $aaa[5], 'type' => $typeArr[1], 'port' => $typeArr[0]];

                if (empty(Db::table('host_port')->where($data)->find())) {
                    Db::table('host_port')->save($data);
                }
            }
        }
    }

    public static function updateScanTime($id)
    {
        $data = ['ip_scan_time' => date('Y-m-d H:i:s', time())];
        Db::name('host')->where('id', $id)->update($data);
    }
}
